package com.lfs.market.utils;

import com.lfs.market.model.JobBean;
import org.quartz.*;

/**
 * @author lfs
 * @date 2025/6/30 17:20:22
 */
public class JobUtils {
    /**
     * 创建一个定时任务
     *
     * @param scheduler 调度器
     * @param jobBean   任务Bean
     */
    public static void createJob(Scheduler scheduler, JobBean jobBean) {
        Class<? extends Job> jobClass;
        JobDetail jobDetail;
        Trigger trigger;
        try {
            jobClass = (Class<? extends Job>) Class.forName(jobBean.getJobClass());
            jobDetail = JobBuilder.newJob(jobClass)
                    .storeDurably()// 启用持久化
                    .withIdentity(jobBean.getJobName())
                    .usingJobData("count", 1)
                    .build();

            trigger = TriggerBuilder.newTrigger()
                    .withIdentity(jobBean.getJobName() + "_trigger")// 唯一标识
                    .forJob(jobDetail)
                    .withSchedule(CronScheduleBuilder.cronSchedule(jobBean.getCronExpression()))
                    .build();
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 暂停任务
     *
     * @param scheduler
     * @param jobName
     */
    public static void pauseJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复任务
     *
     * @param scheduler
     * @param jobName
     */
    public static void resumeJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除任务
     *
     * @param scheduler
     * @param jobName
     */
    public static void deleteJob(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行一次任务，需要先创建一个任务才能执行
     *
     * @param scheduler
     * @param jobName
     */
    public static void runJobOnce(Scheduler scheduler, String jobName) {
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.triggerJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改任务
     *
     * @param scheduler
     * @param jobBean
     */
    public static void modifyJob(Scheduler scheduler, JobBean jobBean) {
        // 1.获取任务触发器的唯一标识
        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobName() + "_trigger");
        try {
            // 2.通过唯一标识获取旧的触发器对象
            CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            // 3.使用新corn表达式构建新的触发器
            String newCron = jobBean.getCronExpression();
            CronTrigger newTrigger = oldTrigger.getTriggerBuilder()
                    .withSchedule(CronScheduleBuilder.cronSchedule(newCron).withMisfireHandlingInstructionDoNothing())
                    .build();
            // 4.调度器更新任务的触发器
            scheduler.rescheduleJob(triggerKey, newTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
